package com.morizhang.merchanmanager.api.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.morizhang.merchanmanager.api.ApiResult;
import com.morizhang.merchanmanager.api.IService;
import com.morizhang.merchanmanager.api.model.WarehouseShelfModel;
import com.morizhang.merchanmanager.api.plugins.BasicWareManage;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.Method;
import java.util.List;

@ServiceAnnotation(name = "WarehouseShelfManageService")
public class WarehouseShelfManageService implements IService {
    @Autowired
    private BasicWareManage basicWareManage;
    @Override
    public ApiResult callService(JSONObject requestParams) throws Exception {
        String callMethodName = requestParams.getString("requestMethod");
        Method callMethod = this.getClass().getMethod(callMethodName, JSONObject.class);
        ApiResult result = (ApiResult)callMethod.invoke(this, requestParams);
        result.setSuccess(true);
        return result;
    }

    public ApiResult selectAllWarehouse(JSONObject requestParams){
        List<WarehouseShelfModel> resultList = basicWareManage.selectAllWarehouse();
        JSONArray resultJsonArray = JSON.parseArray(JSON.toJSONString(resultList));
        ApiResult result = new ApiResult();
        result.setData(resultJsonArray);
        return result;
    }

    public ApiResult queryWarehouseInfo(JSONObject requestParams){
        String warehouseName  = requestParams.getJSONObject("data").getString("warehouseName");
        WarehouseShelfModel resultModel = basicWareManage.queryWarehouseInfo(warehouseName);
        JSONObject resultJsonObject = JSON.parseObject(JSON.toJSONString(resultModel));
        ApiResult result = new ApiResult();
        result.setData(resultJsonObject);
        return result;
    }

    public ApiResult addWarehouseInfo(JSONObject requestParams) throws Exception {
        JSONObject warehouseInfo = requestParams.getJSONObject("data");
        WarehouseShelfModel warehouseModel = JSON.toJavaObject(warehouseInfo, WarehouseShelfModel.class);
        basicWareManage.addWarehouseInfo(warehouseModel);
        return new ApiResult();
    }

    public ApiResult deleteWarehouseInfo(JSONObject requestParams){
        String warehouseName  = requestParams.getJSONObject("data").getString("warehouseName");
        basicWareManage.deleteWarehouseInfo(warehouseName);
        return new ApiResult();
    }

    public ApiResult updateWarehouseInfo(JSONObject requestParams){
        JSONObject warehouseInfo  = requestParams.getJSONObject("data");
        WarehouseShelfModel warehouseModel = JSON.toJavaObject(warehouseInfo, WarehouseShelfModel.class);
        basicWareManage.updateWarehouseInfo(warehouseModel);
        return new ApiResult();
    }

    public ApiResult selectAllWarehouseSection(JSONObject requestParams){
        String warehouseNo  = requestParams.getJSONObject("data").getString("warehouseNo");
        List<WarehouseShelfModel> resultList = basicWareManage.selectAllWarehouseSection(warehouseNo);
        JSONArray resultJsonArray = JSON.parseArray(JSON.toJSONString(resultList));
        ApiResult result = new ApiResult();
        result.setData(resultJsonArray);
        return result;
    }

    public ApiResult queryWarehouseSectionInfo(JSONObject requestParams){
        String warehouseSectionNo  = requestParams.getJSONObject("data").getString("warehouseSectionNo");
        WarehouseShelfModel resultModel = basicWareManage.queryWarehouseSectionInfo(warehouseSectionNo);
        JSONObject resultJsonObject = JSON.parseObject(JSON.toJSONString(resultModel));
        ApiResult result = new ApiResult();
        result.setData(resultJsonObject);
        return result;
    }

    public ApiResult addWarehouseSectionInfo(JSONObject requestParams) throws Exception {
        JSONObject warehouseSectionInfo = requestParams.getJSONObject("data");
        WarehouseShelfModel warehouseModel = JSON.toJavaObject(warehouseSectionInfo, WarehouseShelfModel.class);
        basicWareManage.addWarehouseSectionInfo(warehouseModel);
        return new ApiResult();
    }

    public ApiResult deleteWarehouseSectionInfo(JSONObject requestParams){
        String warehouseSectionName  = requestParams.getJSONObject("data").getString("warehouseSectionName");
        basicWareManage.deleteWarehouseSectionInfo(warehouseSectionName);
        return new ApiResult();
    }

    public ApiResult updateWarehouseSectionInfo(JSONObject requestParams){
        JSONObject warehouseInfo  = requestParams.getJSONObject("data");
        WarehouseShelfModel warehouseSectionModel = JSON.toJavaObject(warehouseInfo, WarehouseShelfModel.class);
        basicWareManage.updateWarehouseSectionInfo(warehouseSectionModel);
        return new ApiResult();
    }

    public ApiResult selectAllWarehouseShelf(JSONObject requestParams){
        String warehouseSectionNo  = requestParams.getJSONObject("data").getString("warehouseSectionNo");
        List<WarehouseShelfModel> resultList = basicWareManage.selectAllWarehouseShelf(warehouseSectionNo);
        JSONArray resultJsonArray = JSON.parseArray(JSON.toJSONString(resultList));
        ApiResult result = new ApiResult();
        result.setData(resultJsonArray);
        return result;
    }

    public ApiResult queryWarehouseShelfInfo(JSONObject requestParams){
        String warehouseShelfNo  = requestParams.getJSONObject("data").getString("warehouseShelfNo");
        WarehouseShelfModel resultModel = basicWareManage.queryWarehouseShelfInfo(warehouseShelfNo);
        JSONObject resultJsonObject = JSON.parseObject(JSON.toJSONString(resultModel));
        ApiResult result = new ApiResult();
        result.setData(resultJsonObject);
        return result;
    }

    public ApiResult addWarehouseShelfInfo(JSONObject requestParams) throws Exception {
        JSONObject warehouseShelfInfo = requestParams.getJSONObject("data");
        WarehouseShelfModel warehouseModel = JSON.toJavaObject(warehouseShelfInfo, WarehouseShelfModel.class);
        basicWareManage.addWarehouseShelfInfo(warehouseModel);
        return new ApiResult();
    }

    public ApiResult deleteWarehouseShelfInfo(JSONObject requestParams){
        String warehouseShelfName  = requestParams.getJSONObject("data").getString("warehouseShelfName");
        basicWareManage.deleteWarehouseShelfInfo(warehouseShelfName);
        return new ApiResult();
    }

    public ApiResult updateWarehouseShelfInfo(JSONObject requestParams){
        JSONObject warehouseShelfInfo  = requestParams.getJSONObject("data");
        WarehouseShelfModel warehouseSectionModel = JSON.toJavaObject(warehouseShelfInfo, WarehouseShelfModel.class);
        basicWareManage.updateWarehouseShelfInfo(warehouseSectionModel);
        return new ApiResult();
    }
}
